package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.StudentSchoolDao;
import com.zzgc.common.entity.StudentSchool;
import com.zzgc.modules.service.StudentSchoolService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生与学校关联信息(StudentSchool)表服务实现类
 *
 * @author makejava
 * @since 2023-02-02 09:12:18
 */
@Service("studentSchoolService")
public class StudentSchoolServiceImpl implements StudentSchoolService {
    @Resource
    private StudentSchoolDao studentSchoolDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StudentSchool queryById(Long id) {
        return this.studentSchoolDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param studentSchool 筛选条件
     * @return 查询结果
     */
    @Override
    public List<StudentSchool> queryByPage(StudentSchool studentSchool) {
        List<StudentSchool> studentSchools = this.studentSchoolDao.queryAllByLimit(studentSchool);
        long total = this.studentSchoolDao.count(studentSchool);
        if (total == 0){
            return new ArrayList<>();
        }
        studentSchools.get(0).setTotal(total);
        return studentSchools;
    }
    /**
     * 查询班级信息， 去重
     *
     * @param studentSchool 筛选条件
     * @return 查询结果
     */
    @Override
    public List<String> queryClass(StudentSchool studentSchool) {
        return this.studentSchoolDao.queryAllClass(studentSchool);
    }

    @Override
    public Long count(StudentSchool studentSchool) {
        return this.studentSchoolDao.count(studentSchool);
    }

    /**
     * 新增数据
     *
     * @param studentSchool 实例对象
     * @return 实例对象
     */
    @Override
    public StudentSchool insert(StudentSchool studentSchool) {
        this.studentSchoolDao.insert(studentSchool);
        return studentSchool;
    }

    /**
     * 修改数据
     *
     * @param studentSchool 实例对象
     * @return 实例对象
     */
    @Override
    public StudentSchool update(StudentSchool studentSchool) {
        this.studentSchoolDao.update(studentSchool);
        return this.queryById(studentSchool.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.studentSchoolDao.deleteById(id) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param studentId 学生id
     * @param year 年份
     * @return 是否成功
     */
    @Override
    public boolean deleteByStudentId(Long studentId, String year) {
        return this.studentSchoolDao.deleteByStudentId(studentId, year) > 0;
    }
}
